package com.flower.subject.application.controller;


import com.flower.subject.application.convert.SubjectCategoryDTOConverter;
import com.flower.subject.application.convert.SubjectLabelDTOConverter;
import com.flower.subject.application.dto.SubjectCategoryDTO;
import com.flower.subject.application.dto.SubjectLabelDTO;
import com.flower.subject.domain.convent.SubjectLabelConverter;
import com.flower.subject.domain.entity.SubjectCategoryBO;
import com.flower.subject.domain.entity.SubjectLabelBO;
import com.flower.subject.domain.service.SubjectCategoryDomainService;
import com.flower.subject.domain.service.SubjectLabelDomainService;
import com.flower.subject.entity.Result;
import com.flower.subject.infra.basic.entity.SubjectCategory;
import com.flower.subject.infra.basic.entity.SubjectLabel;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-02 18:06
 */
@RestController
@RequestMapping("label")
@Slf4j
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        if(log.isInfoEnabled()){

        }

        Preconditions.checkNotNull(subjectLabelDTO.getLabelName(),"标签名字不能为空");
        SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANT.convertBoTOLabel(subjectLabelDTO);
        subjectLabelDomainService.add(subjectLabelBO);


        return Result.ok(true);
    }

//    @PostMapping("/queryPrimaryCategory")
//    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(){
//        SubjectCategory subjectCategory = new SubjectCategory();
//        subjectCategory.setParentId(0);
//        List<SubjectCategoryBO> subjectCategoryBOS = subjectCategoryDomainService.queryPrimaryCategory(subjectCategory);
//        List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANT.convertBoTOCategoryDTOList(subjectCategoryBOS);
//
//        return Result.ok(subjectCategoryDTOS);
//
//    }


    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){

        SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANT.convertBoTOLabel(subjectLabelDTO);
        List<SubjectLabelBO> labelBOS = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
        List<SubjectLabelDTO> subjectCategoryBOS = SubjectLabelDTOConverter.INSTANT.covertLabelBOToDTOList(labelBOS);



        return Result.ok(subjectCategoryBOS);

    }


    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){

            log.info("update category:{}",subjectLabelDTO);
        }
        Preconditions.checkNotNull(subjectLabelDTO.getId(),"ID不能为空");
        SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANT.convertBoTOLabel(subjectLabelDTO);
        Boolean result = subjectLabelDomainService.update(subjectLabelBO);


        return Result.ok(result);

    }


    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        Preconditions.checkNotNull(subjectLabelDTO.getId(),"ID不能为空");
        SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANT.convertBoTOLabel(subjectLabelDTO);
        Boolean result = subjectLabelDomainService.delete(subjectLabelBO);


        return Result.ok(result);

    }




}
